from configparser import ConfigParser



class My_Conf(ConfigParser):

    def __init__(self,file_name,encoding='utf-8'):
        '''
        操作配置文件方法
        :param file_name:   文件名称/文件路径
        :param encoding:    字符编码,默认utf-8
        '''
        super().__init__()
        self.file_name = file_name
        self.encoding = encoding
        self.read(self.file_name,self.encoding)

    #获取字符串
    def get_str(self,section,option):
        res = self.get(section=section,option=option)
        return res

    #获取整数
    def get_int(self,section,option):
        res = self.getint(section=section,option=option)
        return res

    #获取boolea
    def get_boolea(self,section,option):
        res = self.getboolean(section=section,option=option)
        return res

    #获取float
    def get_float(self,section,option):
        res = self.getfloat(section=section,option=option)
        return res

    #写入方法
    def writ_data(self,section,option,value):
       self.set(section=section,option=option,value=value)
       self.write(open(self.file_name,'w',encoding=self.encoding))


if __name__ == '__main__':
    conf = My_Conf(r'F:\projectwork\conf\conf.ini')
    # res = conf.get_str('test','name')
    res = conf.get_str('env','url')
    print(res)
